package com.bawei.weekone2207a.view.home.good

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import coil.compose.AsyncImage
import com.bawei.weekone2207a.base.OnError
import com.bawei.weekone2207a.base.OnLoading
import com.bawei.weekone2207a.base.OnSuccess
import com.bawei.weekone2207a.model.FindCommodityByKeywordEntity
import com.bawei.weekone2207a.route.RouteConfig
import kotlinx.coroutines.launch

/**
 * 作者：赵亮
 * 时间：2024/12/14 9:56
 * 功能：
 */
@Composable
fun GoodPage(
    navController: NavController = rememberNavController(),
    viewModel: GoodViewModel = hiltViewModel()
) {
    // 1. 数据
    val list = remember {
        SnapshotStateList<FindCommodityByKeywordEntity.Result>()
    }
    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {}
                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        list.addAll((it.data as FindCommodityByKeywordEntity).result)
                    }
                }
            }
        }
        launch {
            viewModel.sendIntent(GoodIntent.FindCommodityByKeyword("鞋", 1, 5))
        }
    }
    // 2. 网格展示
    LazyVerticalGrid(
        modifier = Modifier.padding(start = 10.dp, end = 10.dp, top = 24.dp, bottom = 90.dp),
        horizontalArrangement = Arrangement.spacedBy(10.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        columns = GridCells.Fixed(2)
    ) {
        items(list) {
            Card {
                Column (modifier = Modifier.padding(10.dp).clickable {
                    navController.navigate(RouteConfig.DETAIL)
                }){
                    AsyncImage(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(150.dp),
                        contentScale = ContentScale.Crop,
                        model = it.masterPic,
                        contentDescription = null
                    )
                    Text(text = it.commodityName, maxLines = 1)
                    Text(text = "￥：${it.price}", color = Color.Red)
                }
            }
        }
    }

}

@Preview
@Composable
fun GoodPagePreview() {
    GoodPage()
}

